package com.z2wenfa.innosticproducer.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.NameFilter;
import com.z2wenfa.innosticproducer.dao.SalesCountDao;
import com.z2wenfa.innosticproducer.util.JsonWrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//销售统计控制器
@RequestMapping("/salescount")
@RestController
public class SalesCountController {

    @Autowired
    private SalesCountDao salesCountDao;

    @RequestMapping("/areas")
    String getCompanyWithServiceWithHospiatl() {
        List<HashMap<String, Object>> areas = salesCountDao.getCompanyAndServiceAndHospitalMap();

        HashMap<String, Object> allParent = new HashMap<>();
        allParent.put("name", "全部");
        allParent.put("id", -1);

        List<Object> arrObject = new ArrayList<>();
        HashMap<String, Object> allChild = new HashMap<>();
        allChild.put("name", "全部");
        allChild.put("id", -1);
        arrObject.add(allChild);
        allParent.put("rows", arrObject);

        areas.add(0, allParent);

        return JSON.toJSONString(JsonWrapperUtil.wrapperHashMap(areas));
    }


    @RequestMapping("/yearWithMonth")
    String getYearWithMonth() {
        List<HashMap<String, Object>> yearWithMonth = salesCountDao.getYearWithMonth();

        HashMap<String, Object> allParent = new HashMap<>();
        allParent.put("name", "全部");
        allParent.put("id", -1);

        List<Object> arrObject = new ArrayList<>();
        HashMap<String, Object> allChild = new HashMap<>();
        allChild.put("name", "全部");
        allChild.put("id", -1);
        arrObject.add(allChild);
        allParent.put("rows", arrObject);

        yearWithMonth.add(0, allParent);

        return JSON.toJSONString(JsonWrapperUtil.wrapperHashMap(yearWithMonth));
    }

    @RequestMapping("/dataList")
    String getSalesCountData(
            @RequestParam(value = "companyId", required = false, defaultValue = "0") int companyId,
            @RequestParam(value = "serviceId", required = false, defaultValue = "0") int serviceId,
            @RequestParam(value = "yearId", required = false, defaultValue = "0") int yearId,
            @RequestParam(value = "monthId", required = false, defaultValue = "0") int monthId) {
        List<Object> salesCountDataList = salesCountDao.getSalesCountDataList(companyId, serviceId, yearId, monthId);
        return JSON.toJSONString(JsonWrapperUtil.wrapperHashMap(salesCountDataList));
    }


    @RequestMapping("/dataListByarea")
    String getSalesCountDataByArea(@RequestParam(value = "companyId", required = false, defaultValue = "0") int companyId,
                                   @RequestParam(value = "serviceId", required = false, defaultValue = "0") int serviceId,
                                   @RequestParam(value = "yearId", required = false, defaultValue = "0") int yearId,
                                   @RequestParam(value = "monthId", required = false, defaultValue = "0") int monthId) {


        List<Object> dataList = salesCountDao.getSalesCountDataListByArea(companyId, serviceId, yearId, monthId);

        NameFilter nameFilter = null;
        if (serviceId > 0) {
            nameFilter = (object, name, value) -> {
                if (name.equals("hospitalId")) {
                    return "id";
                }
                if (name.equals("hospitalName")) {
                    return "name";
                }
                return name;
            };
        } else if (companyId > 0) {
            nameFilter = (object, name, value) -> {
                if (name.equals("serviceId")) {
                    return "id";
                }
                if (name.equals("serviceName")) {
                    return "name";
                }
                return name;
            };
        }

        if (companyId <= 0) {
            nameFilter = (object, name, value) -> {
                if (name.equals("companyId")) {
                    return "id";
                }
                if (name.equals("companyName")) {
                    return "name";
                }
                return name;
            };
        }


        return JSON.toJSONString(JsonWrapperUtil.wrapperHashMap(dataList), nameFilter);
    }


    //获得各级别的地区的销售统计
    @RequestMapping("/dataListByLevel")
    public String getAreaSalesCountDataList(@RequestParam(value = "companyId", required = false, defaultValue = "0") int companyId,
                                            @RequestParam(value = "serviceId", required = false, defaultValue = "0") int serviceId,
                                            @RequestParam(value = "yearId", required = false, defaultValue = "0") int yearId,
                                            @RequestParam(value = "monthId", required = false, defaultValue = "0") int monthId) {

        List<Object> dataList = null;
        if (companyId > 0 && serviceId > 0) {
            dataList = (salesCountDao.getSalesCountDataListByHospitalLevel(companyId, serviceId, yearId, monthId));
        }

        if (companyId > 0 && serviceId <= 0) {
            dataList = (salesCountDao.getSalesCountDataListByServiceLevel(companyId, serviceId, yearId, monthId));
        }

        if (companyId <= 0) {
            dataList = (salesCountDao.getSalesCountDataListByCompanyLevel(companyId, serviceId, yearId, monthId));
        }


        return JSON.toJSONString(dataList);
    }


}
